$(function(){
	asideClickHandle();
	var page = 1	
	/* 模板添加jQuery函数 */
	template.helper('getJquery',function(){
		return jQuery;
	})
	/*默认展示第一页数据 */
	getcateSecondData({page:page,pageSize:4},function(data){
		console.log(data);
		$("tbody").html(template("list",data))
		// 分页展示
		setPage(data.page,Math.ceil(data.total/data.size) ,function(page){
			getcateSecondData({page:page,pageSize:4},function(data){
				$("tbody").html(template("list",data))
			})
		});
	})
	/*模态框显示与隐藏*/
	modal();
	/*动态创建模态框一级分类*/
	getCateFirstData(function(data){
		$(".dropdown-menu").html(template("dropdown",data)).find("li").on("click",function(){
			var $currA = $(this).children('a')
			$("#categoryName").text($currA.text());
			$("input[name='categoryId']").val($currA.attr("data-id"));
			$("#upload").data('bootstrapValidator').updateStatus("categoryId",'VALID');

		})
	});
	/*初始化上传文件*/
	initUpload();
	/* 校验表单 */
	$('#upload').bootstrapValidator({
		excluded:[],
		feedbackIcon: {
			valid: "glyphicon glyphicon-ok",
			invalid: "glyphicon glyphicon-remove",
			validating: "glyphicon glyphicon-refresh"
		},
		fields: {
			categoryId: {
				/*校验方式*/
				validators: {
					notEmpty: {
						message: "请选择一级分类"
					}
				}
			},
			secondName: {
				validators: {
					notEmpty: {
						message: "请输入二级分类名称"
					}
				}
			},
			brandLogo: {
				validators: {
					notEmpty: {
						message: "请选择一张图片"
					}
				}
			}
		}
	}).on('success.form.bv',function(e){
		e.preventDefault();
		var $form = $(e.target);
		$.ajax({
			url:"/category/addSecondCategory",
			type: "POST",
			data: $form.serialize(),
			dataType:"json",
			success:function(e){
				if(e.success){
					getcateSecondData({page:page,pageSize:4},function(data){
						console.log(data);
						$("tbody").html(template("list",data))
						// 分页展示
						setPage(data.page,Math.ceil(data.total/data.size) ,function(page){
							getcateSecondData({page:page,pageSize:4},function(data){
								$("tbody").html(template("list",data))
							})
						});
					})
				}
			}
		})
	})
})


/*初始化文件上传插件*/
var initUpload = function(){
	$("[name='pic1']").fileupload({
		url:'/category/addSecondCategoryPic',
		dataType:"json",
		done:function(e,data){
			$("#uploadImg").attr('src',data.result.picAddr);
			$("input[name='brandLogo']").val(data.result.picAddr);
			$("#upload").data("bootstrapValidator").updateStatus('brandLogo',"VALID")
		}
	})
}

/*模态框初始化*/
var modal = function(){
	$("#addBtn").on("click",function(){
		$(".add_modal").modal('show');
	})
}

/*获取一级分类数据*/
var getCateFirstData = function(callback){
	$.ajax({
		url:"/category/queryTopCategoryPaging",
		type:"get",
		data:{
			page:1,
			pageSize: 100
		},
		dataType:"json",
		success:function(res){
			callback && callback(res);
		}

	});
}

/*获取二级分类数据*/
var getcateSecondData = function(data,callback){
	$.ajax({
		url:"/category/querySecondCategoryPaging",
		type:"get",
		data:data,
		dataType:"json",
		success:function(res){
			callback && callback(res);
		}
	})	
}

/*设置分类页*/
var setPage = function(pageCurrent,pageSum,callback){
	$('.pagination').bootstrapPaginator({
		// 设置版本号
		bootstrapMajorVersion: 3,
		// 设置分页按钮的大小
		size: "small",
		// 显示第几页
		currentPage: pageCurrent,
		// 总页数
		totalPages: pageSum,
		// 单击操作按钮的时候，执行该函数、调用Ajax渲染页面
		numberOfPages: 5,
		onPageClicked:function(event,originEvent,type,page){
			currentPage = page;
			callback && callback(page);
		}
	})
}